<?php
/*
 * Copyright (c) Madbit 2012. All Rights Reserved.
 * Reproduction in whole or in part is prohibited without the
 * written consent of the copyright owner.
 */

/**
 * Description of LinkControllers
 *
 * @author Alessio Fiore
 */
class LinkController {

    //put your code here

    public function createLink($idc, $title, $url) {
        include_once '../utils/includer.php';
        includeFileOnce("utils/redirect.php");
        includeFileOnce("protected/database/MySqlDAO.php");
        includeFileOnce("protected/messages/it/messages.php");
        
        $messages = getBundleMessages();

        if (!empty($_GET['idc'])) {
            $idc = $_GET['idc'];

            $response = array();

            if (!empty($title)) {
                if (!empty($url)) {

                    $mySqlDAO = new MySqlDAO();

                    /* Check if title is already used */
                    $idlres = $mySqlDAO->checkLinkTitle($idc, $title);
                    if (!empty($idlres)) {
                        $response['status'] = "FAILURE";
                        $response['message'] = $messages['createlink.name_already_used'];
                        return $response;
                    } else {
                        $mySqlDAO->createLink($title, $url, $idc);
                        redirect("index.php?idc=$idc", FALSE);
                    }
                } else {
                    $response['status'] = "FAILURE";
                    $response['message'] = $messages['createlink.missing_url'];
                    return $response;
                }
            } else {
                $response['status'] = "FAILURE";
                $response['message'] = $messages['createlink.missing_title'];
                return $response;
            }
        } else {
            die($messages['admin.missed_parentdir']);
        }
    }

    public function modifyLink($idl, $parent_dir, $title, $url) {
        
        include_once '../utils/includer.php';
        includeFileOnce("utils/redirect.php");
        includeFileOnce("protected/database/MySqlDAO.php");
        includeFileOnce("protected/messages/it/messages.php");
        
        $messages = getBundleMessages();

        $response = array();
         
        $mySqlDAO = new MySqlDAO();

        if (!empty($parent_dir) && !empty($idl)) {

            if (!empty($title)) {
                if (!empty($url)) {
                    
                    $idlres = $mySqlDAO->checkLinkTitle($parent_dir, $title);
                    if (!empty($idlres) && $idlres != $idl) {
                        $response['status'] = "FAILURE";
                        $response['message'] = $messages['createlink.name_already_used'];
                        return $response;
                    } else {
                        $mySqlDAO->modifyLink($idl, $title, $url, $parent_dir);
                        redirect("index.php?idc=$parent_dir", FALSE);
                    }
                } else {
                    $response['status'] = "FAILURE";
                    $response['message'] = $messages['createlink.missing_url'];
                    return $response;
                }
            } else {
                $response['status'] = "FAILURE";
                $response['message'] = $messages['createlink.missing_title'];
                return $response;
            }
        } else {
            die($messages['admin.missed_parentdir']);
        }
    }

}
?>
